﻿Imports DevExpress.XtraEditors
Imports DevExpress.XtraSplashScreen

Public Class TiepNhanBenhNhan
    Dim flagdmbenhnhan As Boolean
    Dim mabenhnhan As String
    Dim indexGrid As Integer
    'Lấy mã bệnh nhân sang MaBenhNhanSearchLookupedit của Tiepnhanbenhnhan
    Private Sub TiepNhanBenhNhan_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
        If flagdmbenhnhan Then
            Me.DMBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMBenhNhan)
            Me.MabenhnhanISearchLookupEdit.EditValue = mabenhnhan
            Me.flagdmbenhnhan = False
        End If
    End Sub

    Private Sub TiepNhanBenhNhan_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.DMPhongTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMPhong)
        Me.DMBacSiTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMBacSi)
        Me.DMDanTocTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMDanToc)
        Me.DMBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMBenhNhan)
        Me.DMTinhTrangTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMTinhTrang)
        Me.DMKhamBenhTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMKhamBenh)
        Me.DMPhuongXaTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMPhuongXa)
        Me.DMQuanHuyenTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMQuanHuyen)
        Me.DMTinhThanhTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMTinhThanh)
        Me.DMQuocGiaTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMQuocGia)
        Me.DMNganhNgheTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMNganhNghe)
        Me.GioiTinhTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.GioiTinh)
        Me.BenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.BenhNhan)
        Me.TiepNhanBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.TiepNhanBenhNhan)
        Me.IGridControl.ForceInitialize()
        Me.IGridView1.MoveLast()
        Me.setHeaderFooter("Công Ty CP Tư Vấn Giải Pháp ERP Phong Cách Việt", "Lâm Văn Bền, P.Tân Kiểng, Q.7, TP.HCM")
        SplashScreenManager.CloseForm()
    End Sub
    Public Sub New()
        InitializeComponent()

        Me.setObj(Me.PCV_CLINICDataSet_Don, Me.PCV_CLINICDataSet_Don.TiepNhanBenhNhan, Me.TiepNhanBenhNhanBindingSource, TableAdapterManager, Me.TiepNhanBenhNhanTableAdapter, Me.LoaibenhnhanISearchLookupEdit)
        Me.IGridView1.setBatLoi("ma", "not null")
        Me.IGridView1.setBatLoi("masophieu", "not null")
    End Sub
    'Lọc tỉnh thành theo quốc gia
    Private Sub TinhthanhISearchLookupEdit_Enter(sender As Object, e As System.EventArgs) Handles TinhthanhISearchLookupEdit.Enter
        Me.DMTinhThanhBindingSource.Filter = "maquocgia='" & Me.QuocgiaISearchLookupEdit.EditValue.ToString & "'"
    End Sub
    'Lọc Quận huyện theo tỉnh thành
    Private Sub QuanhuyenISearchLookupEdit_Enter(sender As Object, e As System.EventArgs) Handles QuanhuyenISearchLookupEdit.Enter
        Me.DMQuanHuyenBindingSource.Filter = "matinhthanh='" & Me.TinhthanhISearchLookupEdit.EditValue.ToString & "'"
    End Sub
    'Lọc Phường xã theo quận huyện
    Private Sub PhuongxaISearchLookupEdit_Enter(sender As Object, e As System.EventArgs) Handles PhuongxaISearchLookupEdit.Enter
        Me.DMPhuongXaBindingSource.Filter = "maquanhuyen='" & Me.QuanhuyenISearchLookupEdit.EditValue.ToString & "'"
    End Sub
    'Hiện group BHYT khi loại bệnh nhân là BHYT
    Private Sub LoaibenhnhanISearchLookupEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles LoaibenhnhanISearchLookupEdit.EditValueChanged
        If state <> 0 And Not IsDBNull(Me.LoaibenhnhanISearchLookupEdit.EditValue) And Not IsNothing(Me.LoaibenhnhanISearchLookupEdit.EditValue) Then
            If Me.LoaibenhnhanISearchLookupEdit.EditValue.ToString = "BHYT" Then
                Me.LayoutControlGroupBHYT.Enabled = True
                Me.SobhytITextEdit.Properties.ReadOnly = False
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = False
                Me.NgayhethanIDateEdit.Properties.ReadOnly = False
                Me.TraituyenICheckEdit.Properties.ReadOnly = False
            Else
                Me.LayoutControlGroupBHYT.Enabled = False
                Me.SobhytITextEdit.Properties.ReadOnly = True
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = True
                Me.NgayhethanIDateEdit.Properties.ReadOnly = True
                Me.TraituyenICheckEdit.Properties.ReadOnly = True
            End If
        End If
    End Sub
    ' Lấy từ tỉnh thành, quận huyện, phường xã, ... qua thành Địa chỉ
    Private Sub DiachiITextEdit_GotFocus(sender As Object, e As System.EventArgs) Handles DiachiITextEdit.GotFocus
        If state <> 0 And Not (IsDBNull(Me.PhuongxaISearchLookupEdit.EditValue) AndAlso IsDBNull(Me.QuanhuyenISearchLookupEdit.EditValue) AndAlso IsDBNull(Me.TinhthanhISearchLookupEdit.EditValue) AndAlso IsDBNull(Me.ThonphoduongITextEdit.EditValue) AndAlso IsDBNull(Me.SonhaITextEdit.EditValue)) Then
            Me.DiachiITextEdit.EditValue = " " & Me.SonhaITextEdit.EditValue & ", " & Me.ThonphoduongITextEdit.EditValue & ", " & Trim(Me.PhuongxaISearchLookupEdit.Properties.GetDisplayValueByKeyValue(Me.PhuongxaISearchLookupEdit.EditValue)) & ", " & Trim(Me.QuanhuyenISearchLookupEdit.Properties.GetDisplayValueByKeyValue(Me.QuanhuyenISearchLookupEdit.EditValue)) & ", " & Trim(Me.TinhthanhISearchLookupEdit.Properties.GetDisplayValueByKeyValue(Me.TinhthanhISearchLookupEdit.EditValue))
            Me.DiachiITextEdit.EditValue = LTrim(Me.DiachiITextEdit.EditValue.ToString.Replace(" ,", ""))
        End If
    End Sub

    Public Overrides Sub setSubSau_Sua()
        If Not IsDBNull(Me.LoaibenhnhanISearchLookupEdit.EditValue) Then
            If Me.LoaibenhnhanISearchLookupEdit.EditValue.ToString = "BHYT" And Not IsNothing(Me.LoaibenhnhanISearchLookupEdit.EditValue) Then
                Me.LayoutControlGroupBHYT.Enabled = True
                Me.SobhytITextEdit.Properties.ReadOnly = False
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = False
                Me.NgayhethanIDateEdit.Properties.ReadOnly = False
                Me.TraituyenICheckEdit.Properties.ReadOnly = False
            Else
                Me.LayoutControlGroupBHYT.Enabled = False
                Me.SobhytITextEdit.Properties.ReadOnly = True
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = True
                Me.NgayhethanIDateEdit.Properties.ReadOnly = True
                Me.TraituyenICheckEdit.Properties.ReadOnly = True
            End If
        End If

    End Sub

    Public Overrides Sub setSubSau_TaoMoi()
        Me.setMacDinhCotAn("ma", "TNBN" & System.Guid.NewGuid().ToString)
        Me.NgaytiepnhanIDateEdit.EditValue = Today

        'Me.QuocgiaISearchLookupEdit.EditValue = "VN"
        Me.LoaibenhnhanISearchLookupEdit.Focus()
        Me.LoaibenhnhanISearchLookupEdit.ShowPopup()
    End Sub
    'Tính năm sinh khi tuổi thay đổi
    Private Sub TuoiITextEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles TuoiITextEdit.EditValueChanged
        If Me.state <> 0 AndAlso Not IsDBNull(Me.TuoiITextEdit.EditValue) AndAlso Not IsNothing(Me.TuoiITextEdit.EditValue) AndAlso Me.TuoiITextEdit.IsNeedFocus Then
            Dim namhientai As Integer = CInt(Now.Year)
            Me.NamsinhITextEdit.EditValue = namhientai - CInt(TuoiITextEdit.EditValue)
        End If
    End Sub

    Private Shadows Sub BarButtonItemBaoCao_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemBaoCao.ItemClick
        Dim report As XtraReport_TiepNhanBenhNhan = New XtraReport_TiepNhanBenhNhan
        report.TiepNhanBenhNhanTableAdapter1.FillMa_TiepNhanBenhNhan(report.PcV_CLINICDataSet_Don1.TiepNhanBenhNhan, Me.IGridView1.GetFocusedRowCellValue("ma"))
        'report.NgayThangNam.Value = "Ngày " & Me.NgaytiepnhanIDateEdit.EditValue.ToString.Substring(0, 2) & " tháng " & Me.NgaytiepnhanIDateEdit.EditValue.ToString.Substring(3, 2) & " năm " & Me.NgaytiepnhanIDateEdit.EditValue.ToString.Substring(6, 4)
        report.NgayThangNam.Value = "Ngày " & Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue), "yyyy")
        If IsDBNull(Me.NoigioithieuITextEdit.EditValue) OrElse Me.NoigioithieuITextEdit.EditValue.ToString.Trim = "" Then
            report.HinhThucDen.Value = "Tự đến"
        Else
            report.HinhThucDen.Value = "Chuyển viện"
        End If
        report.ShowRibbonPreview()
    End Sub

    Public Overrides Sub setSubTruoc_TaoMoi()
        Me.BarCheckItemNhapTuDanhSach.Checked = False
    End Sub
    'Tính tuổi khi năm sinh thay đổi
    Private Sub NamsinhITextEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles NamsinhITextEdit.EditValueChanged
        If Me.state <> 0 AndAlso Not IsDBNull(Me.NamsinhITextEdit.EditValue) AndAlso Not IsNothing(Me.NamsinhITextEdit.EditValue) AndAlso Me.NamsinhITextEdit.IsNeedFocus AndAlso Not Me.NamsinhITextEdit.EditValue.ToString = "" AndAlso CInt(Me.NamsinhITextEdit.EditValue) <= CInt(Now.Year) Then
            Dim namhientai As Integer = CInt(Now.Year)
            Me.TuoiITextEdit.EditValue = namhientai - CInt(NamsinhITextEdit.EditValue)
        End If
    End Sub

    Private Sub MabenhnhanISearchLookupEdit_AddNewValue(sender As Object, e As DevExpress.XtraEditors.Controls.AddNewValueEventArgs) Handles MabenhnhanISearchLookupEdit.AddNewValue
        DMBenhNhan.Show()
        DMBenhNhan.MdiParent = Me.MdiParent
        DMBenhNhan.WindowState = FormWindowState.Maximized
        Me.flagdmbenhnhan = True
    End Sub
    'Kiểm tra bệnh nhân đã tồn tại
    Private Function getKiemTraMaTonTaiGrid(ByVal sMaBenhNhan As String) As Boolean 'kiểm tra sự tồn tại của mã - dùng kiểm tra chung với igridview
        'Me.TiepNhanBenhNhanBindingSource.EndEdit()
        Dim acc As Integer
        For index = 0 To Me.IGridView1.RowCount - 2
            If Me.IGridView1.GetRowCellValue(index, "mabenhnhan").ToString.ToUpper = sMaBenhNhan.ToUpper Then
                acc = acc + 1
                Me.indexGrid = index
            End If
        Next
        If acc > 0 Then
            Return True
        End If
        Return False
    End Function
    'Tính số lần đến và số lần nằm viện
    Private Sub MabenhnhanISearchLookupEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles MabenhnhanISearchLookupEdit.EditValueChanged
        Try
            If Me.state <> 0 And Me.getKiemTraMaTonTaiGrid(Me.MabenhnhanISearchLookupEdit.EditValue.ToString) Then
                If Not IsDBNull(Me.IGridView1.GetRowCellValue(indexGrid, "solanden") AndAlso Not Me.IGridView1.GetRowCellValue(indexGrid, "solanden")).ToString = "" AndAlso Not IsDBNull(Me.IGridView1.GetRowCellValue(indexGrid, "solannamvien") AndAlso Not Me.IGridView1.GetRowCellValue(indexGrid, "solannamvien")).ToString = "" Then
                    Me.SolandenITextEdit.EditValue = CInt(Me.IGridView1.GetRowCellValue(indexGrid, "solanden")) + 1
                    Me.SolannamvienITextEdit.EditValue = CInt(Me.IGridView1.GetRowCellValue(indexGrid, "solannamvien")) + 1
                    Me.NgaykhamgannhatIDateEdit.EditValue = Me.IGridView1.GetRowCellValue(indexGrid, "ngaytiepnhan")
                End If
            Else
                Me.SolandenITextEdit.EditValue = 1
                Me.SolannamvienITextEdit.EditValue = 1
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub MabenhnhanISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MabenhnhanISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.MabenhnhanISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TenITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ten")
        Me.DantocISearchLookupEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "dantoc")
        Me.GioitinhISearchLookupEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "gioitinh")
        Me.NgaythangnamsinhIDateEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ngaythangnamsinh")
        Me.NamsinhITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "namsinh")
        Me.SobhytITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "sobhyt")
        Me.NgaybatdauIDateEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ngaybatdau")
        Me.NgayhethanIDateEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ngayhethan")
        Me.TraituyenICheckEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "traituyen")
        Me.NoicongtacITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "noicongtac")
        Me.NghenghiepISearchLookupEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "nghenghiep")
        Me.QuocgiaISearchLookupEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "quocgia")
        Me.TinhthanhISearchLookupEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "tinhthanh")
        Me.QuanhuyenISearchLookupEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "quanhuyen")
        Me.PhuongxaISearchLookupEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "phuongxa")
        Me.ThonphoduongITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "thonphoduong")
        Me.SonhaITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "sonha")
        Me.DiachiITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "diachi")
        Me.DienthoaiITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "dienthoai")
        Me.EmailITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "email")
        'Me.LoaibenhnhanISearchLookupEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "loai")

    End Sub

    Public Sub setMaBenhNhan(ByVal smabenhnhan As String)
        Me.mabenhnhan = smabenhnhan
    End Sub
    'Fill DMBenhNhan theo loại bệnh nhân
    Private Sub MabenhnhanISearchLookupEdit_Enter(sender As Object, e As System.EventArgs) Handles MabenhnhanISearchLookupEdit.Enter
        Me.DMBenhNhanTableAdapter.FillLoaiBenhNhan_DMBenhNhan(Me.PCV_CLINICDataSet_Don.DMBenhNhan, Me.LoaibenhnhanISearchLookupEdit.EditValue)
    End Sub
    'Lấy năm sinh từ NgaythangnamsinhIDateEdit
    Private Sub NgaythangnamsinhIDateEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles NgaythangnamsinhIDateEdit.EditValueChanged
        If state <> 0 AndAlso Not IsDBNull(Me.NgaythangnamsinhIDateEdit.EditValue) AndAlso Not IsNothing(Me.NgaythangnamsinhIDateEdit.EditValue) AndAlso Me.NgaythangnamsinhIDateEdit.EditValue.ToString <> "" AndAlso Me.NgaythangnamsinhIDateEdit.IsNeedFocus Then
            Me.NamsinhITextEdit.EditValue = Format(CDate(Me.NgaythangnamsinhIDateEdit.EditValue), "yyyy")
        End If
    End Sub
End Class
